package per.liu.web;

import org.apache.shiro.cas.CasFilter;
import org.jasig.cas.client.session.SingleSignOutHandler;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by liuj on 2015/6/25.
 */
public class MyCasFilter extends CasFilter {

    private static final String logoutParameterName = "logoutRequest";
    private static final String artifactParameterName = "ticket";

    private static final SingleSignOutHandler handler = new SingleSignOutHandler();

    @Override
    protected void onFilterConfigSet() throws Exception {
        super.onFilterConfigSet();
        handler.setArtifactParameterName(artifactParameterName);
        handler.setLogoutParameterName(logoutParameterName);
        handler.init();
    }


    @Override
    protected void postHandle(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        final HttpServletRequest request = (HttpServletRequest) servletRequest;
        if (handler.isTokenRequest(request)) {
            handler.recordSession(request);
        } else if (handler.isLogoutRequest(request)) {
//            Subject subject = SecurityUtils.getSubject();
//            subject.logout();
            handler.destroySession(request);
            System.out.println("===============LOGOUT=================");
            return;
        } else {
            System.out.println("Ignoring URI " + request.getRequestURI());
        }
        super.postHandle(servletRequest, servletResponse);
    }

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        return super.preHandle(request, response);
    }
}
